import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';

import set from '@dfeidao/atom-web/local/set';
import render from '@dfeidao/atom-web/render/render';
import tree from '@dfeidao/fd-w000001';
import P06 from './P06';

// import P04 from './P04';
export default async function a010(fd: IFeidaoAiBrowserComponent, args?: {
	data: {
		data: {
			client_type: string
			productid: string;
			formid: string;
			formname: string;
			level_no: string;
			group_number: string;
			no: string;
			group_name: string;
		}
	}
}) {
	// 折叠树绑定事件
	const level_no1 = get(fd, 'level_no111') as string;
	// debugger;
	const productid1 = get(fd, 'productid') as string;

	if (!level_no1) {
		error('请先选择一个等级！');
		return;
	} else {
		const zds = fd.data.node.querySelector('[zds]') as tree;

		// debugger;
		const a = get(fd, 'origin_obj') as Map<string, { left: {}, right: Array<{}> }>;
		// console.log(a, 'dd');
		const checked = zds.get_checked();
		const data = args.data.data;
		// const group_number = data.group_number;
		// const client_type1 = fd.data.node.querySelector<HTMLSelectElement>('[select]').value;
		// const res1 = await nodejs<{ dataArray: Array<{ formid: string }> }>('level-inf/zj-000001/s013', { filter: { productid: productid1, client_type: client_type1, group_number } });
		// let flag = 'true';
		// debugger;
		if (!data.no) {
			const checked1 = zds.get_checked();
			const d_data = checked1.filter((nm) => {
				return nm.no;
			});
			const quanxuanarr = new Array();
			checked1.forEach((v: { group_name: string, group_number: string, no: string }) => {
				if (v.no) {
					quanxuanarr.push(v);
				}
			});
			// quanxuanarr.forEach((o) => {
			// 	d_data = checked1.filter((nm) => {
			// 		return nm.formid === o.formid;
			// 	});
			// });
			console.log(d_data, 'd_data');
			a.clear();
			if (d_data.length !== 0) {
				d_data.forEach((i) => {
					if (i.group_name === "未分组") {
						i.group_name = '';
					}
					if (Array.from(a.keys()).indexOf(i.formid) === -1) {
						a.set(i.formid, { left: { client_type: i.client_type, formid: i.formid, formname: i.formname, productid: productid1, level_no: level_no1 }, right: [] });
					} else {
						const te = a.get(i.formid);
						te.left = { client_type: i.client_type, formid: i.formid, formname: i.formname, productid: productid1, level_no: level_no1 };
					}
				});
			} else {
				// res1.dataArray.forEach((j) => {
				// 	a.delete(j.formid);
				// });
				// a.clear();
			}
			set(fd, 'origin_obj', a);
			// console.log(a);
		} else {
			const c_data = checked.filter((nm) => {
				return nm.formid === data.formid;
			});
			// debugger;
			if (c_data.length !== 0) {
				// console.log(args, '333');

				const res = await nodejs<{ dataArray: Array<{ ode: string, formid: string }> }>('level-inf/zj-000001/s012', { filter: { productid: productid1, formid: data.formid } });
				render(fd, res.dataArray, P06, "P06", "inner");
				res.dataArray.forEach((i) => {
					if (Array.from(a.keys()).indexOf(i.formid) === -1) {
						a.set(i.formid, {
							left: {},
							right: [i]
						});
					} else {
						const te = a.get(i.formid);
						te.right.push(i);
					}
				});
				if (Array.from(a.keys()).indexOf(data.formid) === -1) {
					a.set(data.formid, { left: { client_type: data.client_type, formid: data.formid, formname: data.formname, productid: productid1, level_no: level_no1 }, right: [] });
				} else {
					const te = a.get(data.formid);
					te.left = { client_type: data.client_type, formid: data.formid, formname: data.formname, productid: productid1, level_no: level_no1 };
				}

			} else {
				render(fd, '', P06, "P06", "inner");
				a.delete(data.formid);
			}
			set(fd, 'origin_obj', a);
		}



	}


}
